package indi.kider.server.employee.service;

import indi.kider.common.model.apabus.RentOutline;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.EmployeePart;
import indi.kider.common.model.employee.Enterprise;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.server.apabus.mapper.RentOutlineMapper;
import indi.kider.server.employee.mapper.EmployeeMapper;
import indi.kider.server.employee.mapper.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageUtil;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class EmployeePartServiceImpl implements EmployeePartService{

    @Autowired
    private RentOutlineMapper rentOutlineMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 保存合作员工和房间号分配关系
     * @param employeePart
     * @param employeeId
     * @return
     */
    @Transactional
    public String saveEmployeePart(EmployeePart employeePart, Long employeeId){
        try{
            delEmployeePart(employeePart);
            Long [] houseIds = employeePart.getHouseIds();
            for(Long houseId : houseIds){
                RentOutline roLine = new RentOutline();
                roLine.setEmployeeId(employeeId);
                roLine.setEmployeePartId(employeePart.getEmployeePartId());
                roLine.setRemark("分配");
                Example example = new Example(RentOutline.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("housesId",houseId);
                rentOutlineMapper.updateByExampleSelective(roLine,example);
            }
        }catch (Exception e){
            throw new CoreException("保存失败");
        }
        return "保存分配关系成功";
    }

    /**
     *清除分配关系
     * @param employeePart
     * @return
     */
    public String delEmployeePart(EmployeePart employeePart){
        try{
            RentOutline roLine = new RentOutline();
            roLine.setEmployeePartId(0L);
            Example example = new Example(RentOutline.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("employeePartId",employeePart.getEmployeePartId());
            rentOutlineMapper.updateByExampleSelective(roLine,example);
        }catch (Exception e){
            throw new CoreException("清除失败");
        }
        return "清除该员工分配关系成功";
    }

    /**
     * 查询合作企业租客列表
     *
     * @param request
     * @return
     */
    @Override
    public List<Map> getTenantPartList(PageRequest<TenantQuery> request,Employee employee) {
        PageUtil.start(request);
        request.getQuery().setEmployeePartId(employee.getId());
        return tenantMapper.getTenantPartList(request.getQuery());
    }
    /**
     * 查询合作企业房源列表
     *
     * @param request
     * @return
     */
    @Override
    public List<Map> getPartHouseList(PageRequest<TenantQuery> request,Employee employee) {
        PageUtil.start(request);
        request.getQuery().setEmployeePartId(employee.getId());
        return tenantMapper.getPartHouseList(request.getQuery());
    }

    public List<Employee> getEmployeePartByHouseId(Long houseId) {
        return employeeMapper.getEmployeePartByHouseId(houseId);
    }
}
